import axios from 'axios'
// import { Notice } from 'iview'
// import { Message } from 'iview'
import { Notification,MessageBox } from 'element-ui'

import host from './host'

const instance = axios.create({
	  //withCredentials: false,
    baseURL: host,
    timeout: 15000,
    maxRedirects: 0,
})

// #region http.request 处理 | token什么的
// instance.interceptors.request.use(config => {
//   if (store.getters.token) {
//     config.headers['X-Token'] = getToken()
//   }
//   return config
// }, error => {
//   // Do something with request error
//   console.log(error) // for debug
//   Promise.reject(error)
// })
// #endregion

export default function request(url, options) {
    let requestMethod = options && options.body && options.body.method
    if (requestMethod === undefined) {
        requestMethod = options && options.method
        requestMethod = requestMethod ? requestMethod.toLowerCase() : 'get'
    }

    let data = options && options.body
    if (data !== undefined) {
        /**
         * 当约定 'Content-Type': 'application/json'
         * URLSearchParams 提交数据应该属于不合理状态
         * 如果后端不对request[body]宽松处理，也获取不到数据
         */
        if (!(data instanceof URLSearchParams) && !(data instanceof FormData)) {
            delete data.method
        }
    }

    // let requestMethod = options?.body?.method
    // if(requestMethod === undefined) {
    //   requestMethod = options?.method
    //   requestMethod = requestMethod ? requestMethod.toLowerCase() : 'get'
    // }

    // let data = options?.body
    // if(data !== undefined) {
    //   if(!data instanceof URLSearchParams && !data instanceof FormData){
    //     delete data.method
    //   }
    // }

    return instance[requestMethod](url, data)
        .then(response => response.data)
        .catch(e => {
            // Notice.error({ title: '', desc: e.message })
            Notification.error({ title: '', message: e.message || "未知的错误发生了" , duration : 2000 })
        })
}
